package com.stedu.controller;

import com.stedu.bean.Models;
import com.stedu.bean.RespBean;
import com.stedu.exception.SteduException;
import com.stedu.service.impl.ModelsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/admin/models")
public class ModelsController extends HttpServlet {
    @Autowired
    private ModelsServiceImpl modelsService;

    @PostMapping
    public RespBean insert(@RequestBody Models models) throws SteduException {

        //添加
        if (modelsService.insert(models)) {
         return   RespBean.ok("添加成功~~~");
        } else {
       return   RespBean.error("添加失败，请重新添加");
        }
    }
    @DeleteMapping("/{mid}")
    public RespBean delete(@PathVariable("mid") Integer mid )throws SteduException{
        //删除
        if (modelsService.delete(mid)) {
          return RespBean.ok("删除成功~~~");
        } else {
            return RespBean.error("删除失败，请重新删除");
        }
    }
@PutMapping
    public RespBean update(@RequestBody Models models) throws SteduException {

    if (modelsService.update(models)) {
       return  RespBean.ok("修改成功~~~");
    } else {
       return  RespBean.error("修改失败，请重新修改");
    }
    }
@GetMapping("/{mid}")
    public RespBean selectByMid(@PathVariable("mid") Integer mid){
    Models m= modelsService.selectByMid(mid);
   return RespBean.ok("", m);
    }
@GetMapping
    public RespBean selectAll(){
    List<Models> MOdelstList = modelsService.selectAll();
   return RespBean.ok("", MOdelstList);
}
}